const state = {
  address: uni.getStorageSync('address') ? JSON.parse(uni.getStorageSync('address') || '{}') : {},
  userinfo: uni.getStorageSync('userinfo') ? JSON.parse(uni.getStorageSync('userinfo') || '{}') : {},
  token: uni.getStorageSync('token') || '',
}

const mutations = {
  //添加用户收货地址
  addAddress(state, pyload) {
    state.address = pyload
    this.commit('user/addAddressStorage')
  },
  //地址持久化
  addAddressStorage(state) {
    uni.setStorageSync('address', JSON.stringify(state.address))
  },
  // 更新用户信息
  updateUsaerInfo(state, pyload) {
    //个人是空白名空白头像，这里修改一下
    if (JSON.stringify(pyload) !== '{}') {
      pyload.nickName = '阿诚'
      pyload.avatarUrl = 'http://p1.music.126.net/wBQGdxD08P4UdTp_mIRiUw==/109951166571141277.jpg?param=180y180'
    }
    state.userinfo = pyload
    this.commit('user/addUsaerInfo')
  },
  //用户信息持久化
  addUsaerInfo() {
    uni.setStorageSync('userinfo', JSON.stringify(state.userinfo))
  },
  //更新token
  updateToken(state, pyload) {
    state.token = pyload
    this.commit('user/addToken')
  },
  //token持久化
  addToken() {
    uni.setStorageSync('token', state.token)
  },
  //清除用户信息
  removeUserInfo() {
    this.commit('user/addAddress', {})
    this.commit('user/updateUsaerInfo', {})
    this.commit('user/updateToken', '')
  }
}

const getters = {}

const actions = {}

export default {
  namespaced: true,
  state,
  mutations,
  getters,
  actions
}
